{% extends "base.html" %}

{% block title %}{{ post.title }} - 个人博客{% endblock %}

{% block content %}
<div class="container">
    <div class="row">
        <!-- 文章内容 -->
        <div class="col-lg-8">
            <article class="card shadow-sm mb-4">
                <div class="card-body">
                    <h1 class="card-title mb-3">{{ post.title }}</h1>
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <div class="d-flex align-items-center">
                            {% if post.author.avatar %}
                            <img src="{{ post.author.avatar.url }}" alt="{{ post.author.username }}" class="rounded-circle me-2" width="40" height="40">
                            {% else %}
                            <img src="https://via.placeholder.com/40" alt="{{ post.author.username }}" class="rounded-circle me-2">
                            {% endif %}
                            <div>
                                <a href="{% url 'user_detail' post.author.username %}" class="text-decoration-none">{{ post.author.username }}</a>
                                <div class="text-muted small">
                                    <i class="fas fa-calendar me-1"></i> {{ post.publish_date|date:"Y-m-d H:i" }}
                                </div>
                            </div>
                        </div>
                        <div class="text-muted">
                            <i class="fas fa-eye me-1"></i> {{ post.views }} 次浏览
                            <i class="fas fa-comments ms-2 me-1"></i> {{ post.comments.count }} 条评论
                        </div>
                    </div>

                    {% if post.featured_image %}
                    <div class="text-center mb-4">
                        <img src="{{ post.featured_image.url }}" alt="{{ post.title }}" class="img-fluid rounded">
                    </div>
                    {% endif %}

                    <div class="mb-3">
                        {% if post.category %}
                        <a href="{% url 'category_detail' post.category.slug %}" class="badge bg-primary text-decoration-none">{{ post.category.name }}</a>
                        {% endif %}
                        {% for tag in post.tags.all %}
                        <a href="{% url 'tag_detail' tag.slug %}" class="badge bg-secondary text-decoration-none">{{ tag.name }}</a>
                        {% endfor %}
                    </div>

                    <div class="post-content">
                        {{ post.content|safe }}
                    </div>

                    {% if user == post.author %}
                    <div class="mt-4 d-flex gap-2">
                        <a href="{% url 'post_edit' post.pk %}" class="btn btn-outline-primary">
                            <i class="fas fa-edit me-1"></i> 编辑
                        </a>
                        <a href="{% url 'post_delete' post.pk %}" class="btn btn-outline-danger">
                            <i class="fas fa-trash me-1"></i> 删除
                        </a>
                    </div>
                    {% endif %}
                </div>
            </article>

            <!-- 评论区 -->
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h4 class="mb-0">评论 ({{ comments.count }})</h4>
                </div>
                <div class="card-body">
                    {% if user.is_authenticated %}
                    <form method="post" action="{% url 'add_comment' post.id %}" class="mb-4">
                        {% csrf_token %}
                        <div class="mb-3">
                            <textarea name="content" rows="4" class="form-control" placeholder="写下你的评论..." required id="id_content"></textarea>
                        </div>
                        <button type="submit" class="btn btn-primary">发表评论</button>
                    </form>
                    {% else %}
                    <div class="alert alert-info">
                        请 <a href="{% url 'account_login' %}">登录</a> 后发表评论
                    </div>
                    {% endif %}

                    <div class="comments-list mt-4">
                        {% for comment in comments %}
                        <div class="comment" id="comment-{{ comment.id }}">
                            <div class="d-flex justify-content-between">
                                <div class="d-flex mb-2">
                                    {% if comment.author.avatar %}
                                    <img src="{{ comment.author.avatar.url }}" alt="{{ comment.author.username }}" class="rounded-circle me-2" width="40" height="40">
                                    {% else %}
                                    <img src="https://via.placeholder.com/40" alt="{{ comment.author.username }}" class="rounded-circle me-2">
                                    {% endif %}
                                    <div>
                                        <div class="fw-bold">{{ comment.author.username }}</div>
                                        <div class="text-muted small">{{ comment.created_at|date:"Y-m-d H:i" }}</div>
                                    </div>
                                </div>
                                {% if user.is_authenticated %}
                                <button class="btn btn-sm btn-outline-secondary reply-btn" data-comment-id="{{ comment.id }}">
                                    <i class="fas fa-reply me-1"></i> 回复
                                </button>
                                {% endif %}
                            </div>
                            <div class="mt-2">{{ comment.content }}</div>

                            <!-- 回复表单 -->
                            {% if user.is_authenticated %}
                            <div class="reply-form mt-3" id="reply-form-{{ comment.id }}" style="display: none;">
                                <form method="post" action="{% url 'add_reply' comment.id %}">
                                    {% csrf_token %}
                                    <div class="mb-2">
                                        <textarea name="content" rows="2" class="form-control" placeholder="写下你的回复..." required></textarea>
                                    </div>
                                    <div class="d-flex gap-2">
                                        <button type="submit" class="btn btn-sm btn-primary">提交回复</button>
                                        <button type="button" class="btn btn-sm btn-secondary cancel-reply" data-comment-id="{{ comment.id }}">取消</button>
                                    </div>
                                </form>
                            </div>
                            {% endif %}

                            <!-- 回复列表 -->
                            {% for reply in comment.replies.all %}
                            {% if reply.active %}
                            <div class="reply mt-3" id="reply-{{ reply.id }}">
                                <div class="d-flex mb-2">
                                    {% if reply.author.avatar %}
                                    <img src="{{ reply.author.avatar.url }}" alt="{{ reply.author.username }}" class="rounded-circle me-2" width="30" height="30">
                                    {% else %}
                                    <img src="https://via.placeholder.com/30" alt="{{ reply.author.username }}" class="rounded-circle me-2">
                                    {% endif %}
                                    <div>
                                        <div class="fw-bold">{{ reply.author.username }}</div>
                                        <div class="text-muted small">{{ reply.created_at|date:"Y-m-d H:i" }}</div>
                                    </div>
                                </div>
                                <div>{{ reply.content }}</div>
                            </div>
                            {% endif %}
                            {% endfor %}
                        </div>
                        {% empty %}
                        <div class="alert alert-info">暂无评论，快来发表第一条评论吧！</div>
                        {% endfor %}
                    </div>
                </div>
            </div>
        </div>

        <!-- 侧边栏 -->
        <div class="col-lg-4">
            <!-- 作者信息 -->
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h5 class="card-title mb-0">关于作者</h5>
                </div>
                <div class="card-body">
                    <div class="d-flex align-items-center mb-3">
                        {% if post.author.avatar %}
                        <img src="{{ post.author.avatar.url }}" alt="{{ post.author.username }}" class="rounded-circle me-3" width="64" height="64">
                        {% else %}
                        <img src="https://via.placeholder.com/64" alt="{{ post.author.username }}" class="rounded-circle me-3">
                        {% endif %}
                        <div>
                            <h5 class="mb-0">{{ post.author.username }}</h5>
                            <a href="{% url 'user_detail' post.author.username %}" class="text-decoration-none">查看资料</a>
                        </div>
                    </div>
                    {% if post.author.bio %}
                    <p>{{ post.author.bio|truncatewords:30 }}</p>
                    {% else %}
                    <p class="text-muted">该用户还没有填写个人简介</p>
                    {% endif %}
                </div>
            </div>

            <!-- 相关文章 -->
            {% if related_posts %}
            <div class="card shadow-sm mb-4">
                <div class="card-header bg-light">
                    <h5 class="card-title mb-0">相关文章</h5>
                </div>
                <div class="card-body">
                    <ul class="list-group list-group-flush">
                        {% for related_post in related_posts %}
                        <li class="list-group-item px-0">
                            <a href="{{ related_post.get_absolute_url }}" class="text-decoration-none">{{ related_post.title }}</a>
                            <div class="text-muted small">{{ related_post.publish_date|date:"Y-m-d" }}</div>
                        </li>
                        {% endfor %}
                    </ul>
                </div>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
    // 回复功能
    $(document).ready(function() {
        $('.reply-btn').click(function() {
            var commentId = $(this).data('comment-id');
            $('#reply-form-' + commentId).toggle();
        });

        $('.cancel-reply').click(function() {
            var commentId = $(this).data('comment-id');
            $('#reply-form-' + commentId).hide();
        });
    });
</script>
{% endblock %}

